$(function(){
	resourceTree();
	
	/**
	 * 添加事件
	 */
	$('#datagrid-toolbar #add_link').linkbutton({
		onClick:function(){
			add();
		}
	});
	
	/**
	 * 更新事件
	 */
	$('#datagrid-toolbar #edit_link').linkbutton({
		onClick:function(){
			edit();
		}
	});
	
	/**
	 * 删除事件
	 */
	$('#datagrid-toolbar #cancel_link').linkbutton({
		onClick:function(){
			del();
		}
	});
});

/**
 * 查询
 */
function resourceTree(){
	$.ajax({
		type : "GET",
		url : ctx + "/tree/menu",
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			var setting = {
					check: {
						enable: false,
					},
					data: {
						simpleData: {
							enable: true,
							pIdKey: "pid",
						},
						key:{
							name:'name'
						}
					},
					callback: {
						onClick:authAreaTree
					}
				};
			$.fn.zTree.init($("#resourceTree"), setting, data.content);
			var tree = $.fn.zTree.getZTreeObj("resourceTree");
			var node = tree.getNodeByParam("id", "1");
			tree.selectNode(node);
			if(tree.setting.callback.onClick != null){
				tree.setting.callback.onClick(null, tree.setting.treeId, node);//调用事件
			}
		}
	});
}

function authAreaTree(event,treeId,treeNode){
	getAuthAreaByResourceCode(event,treeId,treeNode);
	$.ajax({
		type : "GET",
		url : ctx + "/tree/authArea",
		dataType : "json",
		data:{resourceCode:treeNode.code},
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			var setting = {
					check: {
						enable: false,
					},
					data: {
						simpleData: {
							enable: true,
							pIdKey: "pid",
						},
						key:{
							name:'name'
						}
					},
					callback: {
						onClick:getAuthArea
					}
				};
			$.fn.zTree.init($("#authAreaTree"), setting, data.content);
/*			var tree = $.fn.zTree.getZTreeObj("authAreaTree");
			var node = tree.getNodeByParam("id", "1");
			tree.selectNode(node);
			if(tree.setting.callback.onClick != null){
				tree.setting.callback.onClick(null, tree.setting.treeId, node);//调用事件
			}*/
		}
	});
}

function getAuthArea(event,treeId,treeNode){
	$.ajax({
		type : "GET",
		url : ctx + "/authArea/query",
		data :{code:treeNode.code},
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			$("#datagrid").datagrid({
				border : false,
				fitColumns : true,
				checkbox : true,
				fit : true,
				remoteSort : false,
				rownumbers : true,
				singleSelect : true,
				toolbar : "#datagrid-toolbar",
				data : data.content
			});
		}
	});
}

function getAuthAreaByResourceCode(event,treeId,treeNode){
	$.ajax({
		type : "GET",
		url : ctx + "/authArea/findByPidAndResourceCode",
		data :{resourceCode:treeNode.code},
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			
			$("#datagrid").datagrid({
				border : false,
				fitColumns : true,
				checkbox : true,
				fit : true,
				remoteSort : false,
				rownumbers : true,
				singleSelect : true,
				toolbar : "#datagrid-toolbar",
				data : data.content
			});
		}
	});
}

/**
 * 添加
 */
function add(){
	var resourceTree = $.fn.zTree.getZTreeObj("resourceTree");
	var nodes = resourceTree.getSelectedNodes();
	if(nodes.length == 0){
		message("请选中资源树节点");
		return;
	}
	$('#dialog').dialog({
	    title: '添加',
	    width: 400,
	    height: 250,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/autharea/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#addform").form('load',{});
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	
	    		var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
            	var data = $("#addform").serializeJson();
            	data.resourceCode = nodes[0].code;
            	var authAreaTree = $.fn.zTree.getZTreeObj("authAreaTree");
            	var authAreaNodes = authAreaTree.getSelectedNodes();
            	if(authAreaNodes.length != 0){
            		data.code = authAreaNodes[0].code;
            		data.pid = authAreaNodes[0].pid;
            	}
            	$.ajax({
            		type : "POST",
            		url : ctx + "/authArea/create",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"添加成功")){
            				return;
            			}
            			var authAreaTree = $.fn.zTree.getZTreeObj("authAreaTree");
            			console.log(authAreaTree);
            			authAreaTree.addNodes(authAreaNodes[0], data.content);
            			$('#datagrid').datagrid('appendRow',data.content);
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 更新
 */
function edit(){
	var row = undefined;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
	    title: '更新',
	    width: 400,
	    height: 250,
	    closed: false,
	    cache: false,
	    modal: true,
	    href:ctx + "/web/system/autharea/add.jsp",
	    onClose:function(){
	    	destroy('dialog','#dialogDiv');
	    },
	    onLoad:function(){
	    	$("#addform").form('load',row);
	    },
	    buttons: [{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
            	var bool = $("#addform").form('enableValidation').form('validate');
            	if(!bool){
            		return;
            	}
    	    	var resourceTree = $.fn.zTree.getZTreeObj("resourceTree");
    	    	var nodes = resourceTree.getSelectedNodes();
            	var data = $("#addform").serializeJson();
        		data.resourceCode = nodes[0].code;
        		data.pid = row.pid;
        		data.code = row.code;
        		data.id = row.id;
            	$.ajax({
            		type : "POST",
            		url : ctx + "/authArea/update",
            		data :data,
            		dataType : "json",
            		success : function(data) {
            			if(!showMsg(data,"修改成功")){
            				return;
            			}
            			
        				var authAreaTree = $.fn.zTree.getZTreeObj("authAreaTree");
            			var node = authAreaTree.getNodeByParam("id", row.id, null);
            			node.name = data.content.name;
            			authAreaTree.updateNode(node);
            			
            			var index = $('#datagrid').datagrid('getRowIndex',row);
            			
            			$('#datagrid').datagrid('updateRow',{
            				index: index,
        					row: data.content
        				});
            			
            			$('#dialog').dialog('close');
            		}
            	});
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
            	$('#dialog').dialog('close');
            }
        }]
	});
}

/**
 * 删除
 */
function del(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	
	$.messager.confirm('提示', '确定要删除吗?', function(bool){
	    if (bool){
	    	$.ajax({
	    		type : "POST",
	    		url : ctx + "/authArea/delete",
	    		data :{id : row.id},
	    		dataType : "json",
	    		success : function(data) {
	    			if(!showMsg(data,"删除成功")){
        				return;
        			}
	    			
	    			var authAreaTree = $.fn.zTree.getZTreeObj("authAreaTree");
        			var node = authAreaTree.getNodeByParam("id", row.id, null);
        			authAreaTree.removeNode(node);
    				
    				var index = $('#datagrid').datagrid('getRowIndex',row);
	    			$("#datagrid").datagrid('deleteRow',index);
	    		}
	    	});
	    }
	});
}